package net.zjitc.controller.admin;

import net.zjitc.pojo.Sale;
import net.zjitc.service.SaleService;
import net.zjitc.vo.DateVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Author admin
 * @DATE 2019/4/10 11:22
 */
@Controller
@RequestMapping("/admin/down")
public class DownController {
    @Autowired
    SaleService saleService;

    @RequestMapping("/download")
    public void exprotSaleInfo(DateVo vo, HttpServletResponse response) throws Exception {
        //从后台拿数据
        List<Sale> list = saleService.findSaleInfo(vo);
        //数据转成excel
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/octet-stream");
         //下载的文件名
        String fileName= "";
        try {
            if (StringUtils.isBlank(vo.getMonth())){
                fileName = URLEncoder.encode(vo.getYear()+"销售排行.xlsx","UTF-8");
            }else {
                fileName = URLEncoder.encode(vo.getYear()+"-"+vo.getMonth()+"销售排行.xlsx","UTF-8");
            }
        }catch (Exception e){
            fileName = "sale.xlsx";
        }
        //乱码问题
         fileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1");
        response.addHeader("Content-disposition","attachment;filename="+fileName);

        XSSFWorkbook wb = new XSSFWorkbook();
         //2.创建一个sheet页
        XSSFSheet sheet = wb.createSheet("startTimeendTime");
        sheet.setDefaultRowHeight((short)(2*256));
        sheet.setColumnWidth(0,4000);
        sheet.setColumnWidth(2,4000);
        sheet.setColumnWidth(3,4000);
        sheet.setColumnWidth(4,4000);
        //设置字体
        XSSFFont font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)16);


        //设置首先  //设置列名
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue("商品号");
        cell = row.createCell(1);
        cell.setCellValue("商品名");
        cell = row.createCell(2);
        cell.setCellValue("总销售量");
        cell = row.createCell(3);
        cell.setCellValue("销售总价");


        //根据查询的内容设置
        XSSFRow rows;
        XSSFCell cells;
        for (int i = 0;i<list.size();i++){
            rows = sheet.createRow(i+1);
            cells = rows.createCell(0);
            cells.setCellValue(list.get(i).getPid());

            cells = rows.createCell(1);
            cells.setCellValue(list.get(i).getName());

            cells = rows.createCell(2);
            cells.setCellValue(list.get(i).getNumCount());

            cells = rows.createCell(3);
            Double priceCount = list.get(i).getPriceCount();
            if (priceCount == null){
                cells.setCellValue(0);
            }else {
                cells.setCellValue(list.get(i).getPriceCount());
            }

        }

        try {
            ServletOutputStream out = response.getOutputStream();
            wb.write(out);
            out.close();
            wb.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("downLoadUI")
    public String downloadUI(){
        return "/admin/products/download.jsp";
    }
}
